layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var Cabinet = {
        tableId: "cabinetTable",    //表格id
        condition: {
        	nickname: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Cabinet.initColumn = function () {
        return [[
        	{type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
        	{field: 'cabinetId',sort: true, title: '机柜编号'},
        	{field: 'cabinetName', sort: true, title: '设备号'},
        	{field: 'useAccount', sort: true, title: '所属人账号'},
            {field: 'address', width: 300, sort: true, title: '地址'},
            {field: 'exceptionType', sort: true, title: '设备状态'},
            {field: 'onlineType', sort: true, title: '在线状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 450}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Cabinet.search = function () {
        var queryData = {};
        queryData['nickname'] = $("#nickname").val();
        table.reload(Cabinet.tableId, {where: queryData, page:{curr: 1}});
    };
    
    /**
     * 弹出添加
     */
    Cabinet.openAddCabinet = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加机柜信息',
            area: ['400px', '510px'],
            content: Feng.ctxPath + '/cabinet/cabinet_add',
            end: function () {
            	Cabinet.search();
            }
        });
    };
    
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openEditCabinet = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改机柜信息',
            area: ['400px', '510px'],
            content: Feng.ctxPath + '/cabinet/cabinet_edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Cabinet.tableId);
            }
        });
    };
    /**
     * 点击弹出小柜子信息
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openSubCabinetList = function (data) {
    	admin.putTempData('formOk', false);
    	top.layui.admin.open({
    		type: 2,
    		title: '柜格设置',
    		area: ['1200px', '600px'],
    		content: Feng.ctxPath + '/cabinet/cabinetSub_list?cabinetId=' + data.cabinetId + '&cabinetName=' + data.cabinetName,
    		end: function () {
    			admin.getTempData('formOk') && table.reload(Cabinet.tableId);
    		}
    	});
    };
    
    /**
     * 点击弹出机柜广告
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openMyAdvert = function (data) {
    	admin.putTempData('formOk', false);
    	top.layui.admin.open({
    		type: 2,
    		title: '我的广告',
    		area: ['600px', '600px'],
    		content: Feng.ctxPath + '/advert/myAdvert?cabinetId=' + data.cabinetId + '&cabinetName=' + data.cabinetName,
    		end: function () {
    			admin.getTempData('formOk') && table.reload(Cabinet.tableId);
    		}
    	});
    };
    
    /**
     * 点击弹出设置机柜广告
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openSetAdvert = function (data) {
    	admin.putTempData('formOk', false);
    	top.layui.admin.open({
    		type: 2,
    		title: '广告设置',
    		area: ['1200px', '600px'],
    		content: Feng.ctxPath + '/advert/setAdvert?cabinetId=' + data.cabinetId + '&cabinetName=' + data.cabinetName,
    		end: function () {
    			admin.getTempData('formOk') && table.reload(Cabinet.tableId);
    		}
    	});
    };
    
    /**
     * 点击弹出资金流水
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openOneOrder = function (data) {
    	admin.putTempData('formOk', false);
    	top.layui.admin.open({
    		type: 2,
    		title: '资金流水',
    		area: ['1200px', '700px'],
    		content: Feng.ctxPath + '/wx_order/oneOrderPage?cabinetId=' + data.cabinetId + '&cabinetName=' + data.cabinetName,
    		end: function () {
    			admin.getTempData('formOk') && table.reload(Cabinet.tableId);
    		}
    	});
    };
    
    /**
     * 点击弹出设置金额信息
     *
     * @param data 点击按钮时候的行数据
     */
    Cabinet.openAmountSetting = function (data) {
    	admin.putTempData('formOk', false);
    	top.layui.admin.open({
    		type: 2,
    		title: '金额设置',
    		area: ['800px', '600px'],
    		content: Feng.ctxPath + '/amountSetting/amountSetting_add?cabinetId=' + data.cabinetId + '&cabinetName=' + data.cabinetName,
    		end: function () {
    			admin.getTempData('formOk') && table.reload(Cabinet.tableId);
    		}
    	});
    };
    
    /**
     * 导出excel按钮
     */
    Cabinet.exportExcel = function () {
        var checkRows = table.checkStatus(Cabinet.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Cabinet.tableId,
        url: Feng.ctxPath + '/cabinet/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Cabinet.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Cabinet.search();
    }); 
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
    	Cabinet.openAddCabinet();
    });
    // 导出excel
    $('#btnExp').click(function () {
    	Cabinet.exportExcel();
    });
    // 工具条点击事件
    table.on('tool(' + Cabinet.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
        	Cabinet.openEditCabinet(data);
        }else if(layEvent === 'subCabinetList'){
        	Cabinet.openSubCabinetList(data);
        }else if(layEvent === 'amountSetting'){
        	Cabinet.openAmountSetting(data);
        }else if(layEvent === 'myAdvert'){
        	Cabinet.openMyAdvert(data);
        }else if(layEvent === 'setAdvert'){
        	Cabinet.openSetAdvert(data);
        }else if(layEvent === 'oneOrder'){
        	Cabinet.openOneOrder(data);
        }
    });
});
